#! /usr/bin/python3
# -*- coding: utf-8 -*-
# __author__ = "Fengxuewei"
# Date: 2020/6/29

# 利用Sample函数随机取样,产生每场进球的球员；用randint函数随机生成每场进球的球员数量
from random import randint, sample
print(sample('abcdefg', 3))

print(sample('abcdefg', randint(3, 5)))


#随机生产三轮比赛的球员进球的统计的字典
d1 = {x:randint(1,4) for x in sample('abcdefg', randint(3, 5))}
d2 = {x:randint(1,4) for x in sample('abcdefg', randint(3, 5))}
d3 = {x:randint(1,4) for x in sample('abcdefg', randint(3, 5))}
print(d1, d2, d3)


# 方法一：通过字典的key进行统计
res = []
for k in d1:
    print(k)
    if (k in d2 and k in d3):
        res.append(k)
print(res)


#方法二：
#####################################################################################################
# step1：使用字典的viewkey()方法，得到一个字典keys的集合
# Step2： 使用map()函数，得到所有字典的keys的集合
# step3： 使用reduce()函数，取所有字典的keys的集合的交集

keys = d1.keys()
print(list(keys))


print(d1.keys() & d2.keys() & d3.keys())


maps = map(dict.keys,[d1,d2,d3])
print(list(maps))


from functools import reduce
print(reduce(lambda a ,b : a & b , map(dict.keys,[d1,d2,d3])))


